package com.gollum.config;

import com.gollum.common.constant.SystemConstants;
import com.gollum.interceptor.HeaderLocaleChangeInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * 国际化配置类
 *
 * @author Herther
 * @createTime 2022年10月28日 23:47:00
 * @since 1.0.0
 */
@Configuration
public class MessageConfig {



    /**
     * 设置session解析国际化bean
     *
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/10/28 23:50
     */
    @Bean
    public LocaleResolver localeResolver(){
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        //设置默认语言
        sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
        return sessionLocaleResolver;
    }


    @Bean
    public WebMvcConfigurer i18nInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //默认拦截器：请求国际化参数为?lang=en_us
                //自定义header拦截器，请求参数在header中添加lang的键值
                LocaleChangeInterceptor i18nInterceptor = new HeaderLocaleChangeInterceptor();
                i18nInterceptor.setParamName(SystemConstants.LANG);
                registry.addInterceptor(i18nInterceptor).addPathPatterns("/**");
            }
        };
    }

}
